﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;
using Chinesys.Domain.DataMapper;
using Chinesys.Domain.BizRules;
using Chinesys.Domain.InputValidations;

namespace Chinesys.Domain
{
    public static class ProductDomain
    {
        public static List<ProductEntity> GetAll()
        {
            List<ProductEntity> list = new List<ProductEntity>();

            using (ChinesysEntities db = new ChinesysEntities())
            {
                List<Product> result = db.Products.ToList();

                result.ForEach(x => list.Add(ProductDataMapper.ProductFromRowToEntity(x)));
            }
            
            return list;
        }

        public static ProductEntity GetByID(int productID)
        {

            ProductEntity entity;
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Product product = db.Products.SingleOrDefault(x => x.Id == productID);
                entity = ProductDataMapper.ProductFromRowToEntity(product);
            }

            return entity;
        }

        public static ProductEntity Save(ProductEntity product)
        {
            ProductInputValidation validate = new ProductInputValidation(product);

            if (!validate.IsValid)
                throw new InputValidationException("Hubo errores de validación", validate);

            ProductBizRules.OnSave(product);

            if(product.CurrentState == RowState.New)
            {
                product = Insert(product);
            }

            if (product.CurrentState == RowState.Modified)
            {
                product = Update(product);
            }

            return product;
        }

        public static ProductEntity Insert(ProductEntity prod)
        {
            Product row = ProductDataMapper.ProductFromEntityToRow(prod);
            using (ChinesysEntities db = new ChinesysEntities())
            {                
                db.AddToProducts(row);
                db.SaveChanges();
                prod.Id = row.Id;
            }
            return prod;
        }

        public static ProductEntity Update(ProductEntity prod)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Product row = db.Products.SingleOrDefault(x => x.Id == prod.Id);

                if (row == null)
                    throw new BizRulesException("El producto no se encuentra registrado, no se puede generar la actualizacion");

                row.Active = prod.Active;
                row.CategoryId = prod.CategoryId;
                row.Description = prod.Description;
                row.ImageSrc = prod.ImageSrc;
                row.Name = prod.Name;
                row.Price = prod.Price;
                row.Rating = prod.Rating;
                row.SubCategoryId = prod.SubCategoryId;
                row.TotalReviews = prod.TotalReviews;

                prod = ProductDataMapper.ProductFromRowToEntity(row);

                db.SaveChanges();
            }

            return prod;
        }
    }
}
